package cn.edu.pku.ss.situationperceiver;

import cn.edu.pku.ss.situationperceiver.ContextData.ContextType;

public class ContextInterpreter {
	/**
	 * Module: ContextInterpreter 
	 * Responsibilities: 	1. Process interpret request from other modules(ContextSharingRequest/ContextSharingService/SRA); 
	 * 						2. Transfer the result to SRA or use it in building ContextSharingRequest;
	 * 						3. Mapping from (context type + truth value) to situation and vice versa;
	 **/
	public static void interpretContextToSituation(ContextType contextType,
			String truthValue) {
		/*
		 * This method works for interpret context into situation and transfer
		 * the result to SRA.
		 */
		switch(contextType)
		{
		case FLOOR_WEIGHT:
			if(Integer.parseInt(truthValue) > 200)
			{
				SituationReasoner.getSituation(1).setVerified(true);
				SituationReasoner.getSituation(1).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(1).setVerified(true);
				SituationReasoner.getSituation(1).setExistence(false);
			}
			break;
		case HAS_COFFEE_CUP:
			if(truthValue.equals("true"))
			{
				SituationReasoner.getSituation(2).setVerified(true);
				SituationReasoner.getSituation(2).setExistence(true);
			}
			else if(truthValue.equals("false"))
			{
				SituationReasoner.getSituation(2).setVerified(true);
				SituationReasoner.getSituation(2).setExistence(false);
			}
			break;
		case COFFEE_TEMPERATURE:
			if(Integer.parseInt(truthValue) > 50)
			{
				SituationReasoner.getSituation(3).setVerified(true);
				SituationReasoner.getSituation(3).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(3).setVerified(true);
				SituationReasoner.getSituation(3).setExistence(false);
			}
			break;
		case IS_MEMBER:
			if(truthValue.equals("true"))
			{
				SituationReasoner.getSituation(4).setVerified(true);
				SituationReasoner.getSituation(4).setExistence(true);
			}
			else if(truthValue.equals("false")) 
			{
				SituationReasoner.getSituation(4).setVerified(true);
				SituationReasoner.getSituation(4).setExistence(false);
			}
			break;
		case LOCATION:
			if(truthValue.equals("meeting room"))
			{
				SituationReasoner.getSituation(7).setVerified(true);
				SituationReasoner.getSituation(7).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(7).setVerified(true);
				SituationReasoner.getSituation(7).setExistence(false);
			}
			break;
		case NUMBER_OF_PEOPLE_IN_ROOM:
			if(Integer.parseInt(truthValue) > 1)
			{
				SituationReasoner.getSituation(8).setVerified(true);
				SituationReasoner.getSituation(8).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(8).setVerified(true);
				SituationReasoner.getSituation(8).setExistence(false);
			}
			break;
		case DIARY:
			if(truthValue.equals("meeting"))
			{
				SituationReasoner.getSituation(5).setVerified(true);
				SituationReasoner.getSituation(5).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(5).setVerified(true);
				SituationReasoner.getSituation(5).setExistence(false);
			}
			break;
		case ROOM_LIGHT:
			if(truthValue.equals("on"))
			{
				SituationReasoner.getSituation(9).setVerified(true);
				SituationReasoner.getSituation(9).setExistence(true);
			}
			else if(truthValue.equals("off"))
			{
				SituationReasoner.getSituation(9).setVerified(true);
				SituationReasoner.getSituation(9).setExistence(false);
			}
			break;
		case PROJECTOR:
			if(truthValue.equals("on"))
			{
				SituationReasoner.getSituation(10).setVerified(true);
				SituationReasoner.getSituation(10).setExistence(true);
			}
			else if(truthValue.equals("off"))
			{
				SituationReasoner.getSituation(10).setVerified(true);
				SituationReasoner.getSituation(10).setExistence(false);
			}
			break;
		case SOUND_METER:
			if(Integer.parseInt(truthValue) > 5)
			{
				SituationReasoner.getSituation(17).setVerified(true);
				SituationReasoner.getSituation(17).setExistence(true);
			}
			else
			{
				SituationReasoner.getSituation(17).setVerified(true);
				SituationReasoner.getSituation(17).setExistence(false);
			}
			break;
		}
	}

	public static void requestSituation(int situationID) {
		/*
		 * This method works for SRA to interpret a situation and use the result
		 * to build a ContextSharingRequest. (ContextSharingService is in charge
		 * of express the interest over the opportunistic network.)
		 */
		ContextType contextType = ContextType.NONE;
		switch(situationID)
		{
		case 1:
			contextType = ContextType.FLOOR_WEIGHT;
			break;
		case 2:
			contextType = ContextType.HAS_COFFEE_CUP;
			break;
		case 3:
			contextType = ContextType.COFFEE_TEMPERATURE;
			break;
		case 4:
			contextType = ContextType.IS_MEMBER;
			break;
		case 5:
			contextType = ContextType.DIARY;
			break;
		case 7:
			contextType = ContextType.LOCATION;
			break;
		case 8:
			contextType = ContextType.NUMBER_OF_PEOPLE_IN_ROOM;
			break;
		case 9:
			contextType = ContextType.ROOM_LIGHT;
			break;
		case 10:
			contextType = ContextType.PROJECTOR;
			break;
		case 17:
			contextType = ContextType.SOUND_METER;
			break;
		}
		ContextShareManager.queryContext(contextType);
	}
}
